package com.whmnx.chitchat.ui.chat;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.tencent.imsdk.TIMConversationType;
import com.tencent.qcloud.tim.uikit.utils.TUIKitConstants;
import com.whmnx.chitchat.R;
import com.whmnx.chitchat.adapter.ChatListAdapter;
import com.whmnx.chitchat.adapter.ChatListAdapter2;
import com.whmnx.chitchat.base.BaseActivity;
import com.whmnx.chitchat.bean.ChatDetailsBean;
import com.whmnx.chitchat.bean.ChatDtssBean;
import com.whmnx.chitchat.bean.ChatMsgBean;
import com.whmnx.chitchat.bean.ChatMsgDatailBean;
import com.whmnx.chitchat.bean.LoginBean;
import com.whmnx.chitchat.bean.UserBean;
import com.whmnx.chitchat.constants.Constants;
import com.whmnx.chitchat.http.CallBack;
import com.whmnx.chitchat.http.HttpClient;
import com.whmnx.chitchat.utils.KeyboardUtils;
import com.whmnx.chitchat.utils.SPUtil;
import com.whmnx.chitchat.utils.UItils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author : huang
 * date   : 2020/8/28 0028 19:20
 * version: 1.0
 * @author Administrator
 */
public class SearchActivity extends BaseActivity {
    @BindView(R.id.message_search)
    EditText query;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refresh;

    @BindView(R.id.rv_chatMsg)
    RecyclerView rvChatMsg;

    private List<ChatDetailsBean> mDataSource = new ArrayList<>();

    private ChatListAdapter2 mChatListAdapter;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.contact_add_activity2);
        setHead_title(View.GONE);
        tv_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    @Override
    protected void loadData() {
        rvChatMsg.setLayoutManager(new LinearLayoutManager(this));
        rvChatMsg.setItemAnimator(new DefaultItemAnimator());
        mChatListAdapter = new ChatListAdapter2(this);
        rvChatMsg.setAdapter(mChatListAdapter);

        query.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                //点击搜索的时候隐藏软键盘
                // hideKeyboard(EditText);
                // 在这里写搜索的操作,一般都是网络请求数据
                if (!TextUtils.isEmpty(v.getText().toString().trim())) {
                    search(v.getText().toString().trim());
                }
                return true;
            }
            return false;
        });
    }

    private void search(String trim) {
        Map<String, Object> params = new HashMap<>();
        params.put("UserID", SPUtil.getString(Constants.USERID));
        params.put("Page", "1");
        params.put("Rows", "100");
        if (getIntent().getBooleanExtra(TUIKitConstants.GroupType.GROUP, false)){
            params.put("GroupID", getIntent().getStringExtra("toUserId"));
        }else {
            params.put("ToUserID", getIntent().getStringExtra("toUserId"));
        }

        params.put("Con", trim);
        if (getIntent().getBooleanExtra("isTopChat", false)){  //0普通消息；1私密消息；
            params.put("RecordType", "1");
        }else {
            params.put("RecordType", "0");
        }

        HttpClient.get(this, Constants.GETSEARCHCHATRECORD, params, new CallBack<ChatMsgDatailBean>() {
            @Override
            public void onSuccess(ChatMsgDatailBean result) {
               // mDataSource.clear();
               //mDataSource.addAll(result.List);
                 if (result.List.size() > 0){
                     SPUtil.put(Constants.TRIM,trim);
                     mChatListAdapter.replaceAll(result.List, true);
                 }else {
                     SPUtil.put(Constants.TRIM,"");
                     UItils.showToastSafe("暂无聊天记录");
                     mChatListAdapter.addAll(result.List);
                 }
            }
        });
    }



    @OnClick(R.id.tv_cancel)
    public void onViewClicked() {
        KeyboardUtils.hideKeyBoard(this,rvChatMsg);
        finish();
    }
}
